package com.gzhryc.log.services;

import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.log.dao.ManagerVisitRecordDao;
import com.gzhryc.log.dao.db.ManagerVisitRecord;
import com.gzhryc.log.services.dto.ManagerVisitRecordSearch;

import java.sql.SQLException;
import java.util.List;

public class ManagerVisitRecordService {

	public static ManagerVisitRecordService self() {
		return MultiDBTools.getService(ManagerVisitRecordService.class, true);
	}

	public static ManagerVisitRecordService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey,ManagerVisitRecordService.class, true);
	}

	ManagerVisitRecordDao dao;

	public ManagerVisitRecordService(String jdbcKey) {
		this.dao = new ManagerVisitRecordDao(jdbcKey);
	}
	
	public List<ManagerVisitRecord> findBySearch(ManagerVisitRecordSearch search,Integer pageNo,Integer pageLimit){
		Conditions condition = dao.buildConditions(search);
		condition.and().ge("createDate", search.getStartDate());
		condition.and().le("createDate", search.getEndDate());
		condition.desc("createDate");
		try {
			return dao.findByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Long countBySearch(ManagerVisitRecordSearch search) {
		Conditions condition = dao.buildConditions(search);
		condition.and().ge("createDate", search.getStartDate());
		condition.and().le("createDate", search.getEndDate());
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public boolean add(ManagerVisitRecord record) {
		try {
			return dao.insert(record) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteByIds(List<Long> ids) {
		if(ids != null && ids.size() > 0) {
			Conditions condition = new Conditions(ManagerVisitRecord.class);
			condition.and().in("id", ids);
			try {
				return dao.deleteByCondition(condition) > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}
